<template>
  <a-modal  ok-text="确认"
      cancel-text="取消"
    :maskClosable="false"
    centered
    :title="title"
    :visible="visible"
    :destroyOnClose="true"
    footer=""
    width="400"
    @cancel="handCancel"
  >
    <div id="barcode">
      <vue-barcode
        :displayValue="false"
        :value="value"
        :width="1.5"
        :height="50"
      />
      <div class="labelBox">{{ JSON.parse(value).label }}</div>
    </div>
    <div class="modal-footer">
      <a-button type="primary" @click="getQrCodeImage()">下载</a-button>
      <a-button @click="handCancel">取消</a-button>
    </div>
  </a-modal>
</template>

<script>
import VueBarcode from "vue-barcode";
export default {
  data() {
    return {
      title: "",
      visible: false,
      value: "",
    };
  },
  methods: {
    showModal(text, title) {
      this.title = title;
      this.value = text;
      this.visible = true;
    },
    getQrCodeImage() {
      var canvasData = document
        .getElementById("barcode")
        .getElementsByTagName("img");
      var a = document.createElement("a");
      a.href = canvasData[0].src;
      a.download = `${this.title}.png`;
      a.click();
    },
    handCancel() {
      this.visible = false;
    },
  },
  components: {
    VueBarcode,
  },
};
</script>

<style lang="less" scoped>
#qrCode {
  text-align: center;
}
.modal-footer {
  text-align: center;
  margin: 20px 0 0 0;
  button {
    margin: 0 20px;
  }
}
.labelBox {
  text-align: center;
  font-size: 24px;
}
</style>
